package com.rfid.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import com.rfid.tool.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rfid.mapper.RfidCategoryMapper;
import com.rfid.domain.RfidCategory;
import com.rfid.service.IRfidCategoryService;

/**
 * 分类管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-08
 */
@Service
public class RfidCategoryServiceImpl implements IRfidCategoryService 
{
    @Autowired
    private RfidCategoryMapper rfidCategoryMapper;

    /**
     * 查询分类管理
     * 
     * @param id 分类管理主键
     * @return 分类管理
     */
    @Override
    public RfidCategory selectRfidCategoryById(String id)
    {
        return rfidCategoryMapper.selectRfidCategoryById(id);
    }

    /**
     * 查询分类管理列表
     * 
     * @param rfidCategory 分类管理
     * @return 分类管理
     */
    @Override
    public List<RfidCategory> selectRfidCategoryList(RfidCategory rfidCategory)
    {
        return rfidCategoryMapper.selectRfidCategoryList(rfidCategory);
    }

    /**
     * 新增分类管理
     * 
     * @param rfidCategory 分类管理
     * @return 结果
     */
    @Override
    public int insertRfidCategory(RfidCategory rfidCategory)
    {
        return rfidCategoryMapper.insertRfidCategory(rfidCategory);
    }

    /**
     * 修改分类管理
     * 
     * @param rfidCategory 分类管理
     * @return 结果
     */
    @Override
    public int updateRfidCategory(RfidCategory rfidCategory)
    {
        return rfidCategoryMapper.updateRfidCategory(rfidCategory);
    }

    /**
     * 批量删除分类管理
     * 
     * @param ids 需要删除的分类管理主键
     * @return 结果
     */
    @Override
    public int deleteRfidCategoryByIds(String[] ids)
    {
        return rfidCategoryMapper.deleteRfidCategoryByIds(ids);
    }

    /**
     * 删除分类管理信息
     * 
     * @param id 分类管理主键
     * @return 结果
     */
    @Override
    public int deleteRfidCategoryById(String id)
    {
        return rfidCategoryMapper.deleteRfidCategoryById(id);
    }



    @Override
    public List<TreeSelect> buildCategoryTreeByList(){
        List<RfidCategory> list=rfidCategoryMapper.selectRfidCategoryList(null);
        //提取属性
        List<TreeSelect> collect = list.stream().map(TreeSelect::new).collect(Collectors.toList());
        return buildDeptTreeByStream(collect);
    }

    public static List<TreeSelect> buildDeptTreeByStream(List<TreeSelect> trees){
        //获取parentId = 0的根节点
        List<TreeSelect> list = trees.stream().filter(item -> item.getParentId() == 0L).collect(Collectors.toList());
        //根据parentId进行分组
        Map<Long, List<TreeSelect>> map = trees.stream().collect(Collectors.groupingBy(TreeSelect::getParentId));
        recursionFnTree(list, map);
        return list;
    }

    /**
     * 递归遍历节点
     * @param list
     * @param map
     */
    public static void recursionFnTree(List<TreeSelect> list, Map<Long, List<TreeSelect>> map){
        for (TreeSelect treeSelect : list) {
            List<TreeSelect> childList = map.get(treeSelect.getId());
            treeSelect.setChildren(childList);
            if (null != childList && 0 < childList.size()){
                recursionFnTree(childList,map);
            }
        }
    }


}
